import {login,logout} from '../../network/companyUser'
import router from '../../router/index'
import { Message } from 'element-ui'


const getDefaultState = () => {
  return {
    token: localStorage.getItem("company_TOKEN"),
    userName:localStorage.getItem("company_userName"),
    list:[],
    name: '',
    avatar: '',
    //管理员侧边栏状态

  }
}

const state = getDefaultState()

const mutations = {
  SET_USERNAME:(state,userName)=>{
    state.userName=userName
  },
  SET_BREAD:(state,list)=>{
    state.list=list
  },
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
}


const actions = {
  // user login
  login({ commit }, userInfo) {
    const {userName, password} = userInfo
    return new Promise((resolve, reject) => {
      login({ userName: userName.trim(), password: password}).then(response => {
        const { data } = response
        if(data.code==100){
          commit('SET_TOKEN', data.data[1])
          commit('SET_USERNAME',userName)
          localStorage.setItem("company_TOKEN",data.data[1])
          localStorage.setItem("company_userName",userName)
          if(data.data[0].userType==1){
            localStorage.setItem("company_companyName",data.data[0].companyName)
            localStorage.setItem("company_realName",data.data[0].realName);
            localStorage.setItem("company_companyId",data.data[0].companyId);
            router.push({ path: this.redirect || '/home' })
            Message.success("登录成功!欢迎回来~")
          }else if(data.data[0].hasAuth==null){
            Message.warning("您还没认证，请先进行企业用户认证")
            router.push("companyAuth");
          }else if(data.data[0].hasAuth==2){
            Message.warning("您认证没通过，请重新认证")
            router.push("companyAuth");
          }else if(data.data[0].hasAuth==0){
            Message.warning("您已提交审核，请等待邮件通知审核结果");
          }
        }else{
          Message.warning("登录失败")
        }
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },


  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then((res) => {
        // must remove  token  first
        localStorage.removeItem("company_TOKEN")
        localStorage.removeItem("company_userName")
        localStorage.removeItem("company_companyName")
        localStorage.removeItem("company_realName");
        localStorage.removeItem("company_companyId");
        console.log(res);
        Message.success("账号已退出~")
        router.push("/login")
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      // removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

 setBreads({commit},list){
    return new Promise(resolve => {
      commit('SET_BREAD',list)
      resolve()
    })
 }
}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}

